package top.lv666.graduation_design_serve.controller;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.lv666.graduation_design_serve.pojo.Blog;
import top.lv666.graduation_design_serve.service.TopicService;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/topic")
//话题模块
public class TopicController {
    // log4j
    public Logger logger = Logger.getLogger(UserController.class);
    private Map<String, Object> returnMap = new HashMap<>();
    private Map<String, Object> paramMap = new HashMap<>();
    @Autowired
    TopicService topicService;

    /**
     * 获取热搜话题
     */
    @GetMapping("/queryHotSearchTopic")
    public Map<String, Object> queryHotSearchTopic(
            @RequestParam int getPage,
            @RequestParam int getNumber
    ) {
        returnMap.clear();
        try {
            returnMap.put("count", topicService.getCount());
            returnMap.put("data", topicService.getHotSearchTopicList(getPage, getNumber));
            returnMap.put("flag", true);
            returnMap.put("msg", "查询成功！");
        } catch (Exception | Error e) {
            returnMap.put("flag", false);
            returnMap.put("msg", "创建失败！");
            logger.error(e);
        }
        return returnMap;
    }


    /**
     * @apiNote 获取话题详细信息
     */
    @GetMapping("/queryTopicById")
    public Map<String, Object> queryTopicById(
            @RequestParam String Id
    ) {
        returnMap.clear();
        try {
            returnMap.put("data", topicService.getTopicById(Id));
            returnMap.put("flag", true);
            returnMap.put("msg", "查询成功！");
        } catch (Exception | Error e) {
            returnMap.put("flag", false);
            returnMap.put("msg", "创建失败！");
            logger.error(e);
        }
        return returnMap;
    }

    /**
     * @apiNote 获取话题列表
     */
    @GetMapping("/queryTopicList")
    public Map<String, Object> queryTopic(
            @RequestParam int getPage,
            @RequestParam int getNumber
    ) {
        returnMap.clear();
        try {
            returnMap.put("count", topicService.getCount());
            returnMap.put("data", topicService.getTopicList(getPage, getNumber));
            returnMap.put("flag", true);
            returnMap.put("msg", "查询成功！");
        } catch (Exception | Error e) {
            returnMap.put("flag", false);
            returnMap.put("msg", "创建失败！");
            logger.error(e);
        }
        return returnMap;
    }

    @PostMapping("/createTopicBlog")
    public Map<String, Object> createTopicBlog(
            @RequestBody Map<String, Object> map,
            HttpServletRequest request
    ) {
        returnMap.clear();
        map.put("request", request);
        try {
            topicService.createTopicBlog(map);
            returnMap.put("flag", true);
            returnMap.put("msg", "创建成功！");
        } catch (Exception | Error e) {
            returnMap.put("flag", false);
            returnMap.put("msg", "创建失败！");
            logger.error(e);
        }
        return returnMap;
    }

    @PostMapping("/createTopic")
    public Map<String, Object> createTopic(
            @RequestBody Map<String, Object> map,
            HttpServletRequest request
    ) {
        returnMap.clear();
        map.put("request", request);
        try {
            topicService.createTopic(map);
            returnMap.put("flag", true);
            returnMap.put("msg", "创建成功！");
        } catch (Exception | Error e) {
            returnMap.put("flag", false);
            returnMap.put("msg", "创建失败！");
            logger.error(e);
        }
        return returnMap;
    }

    @GetMapping("/getTopicBlogHot")
    public Map<String, Object> getTopicBlog(
            @RequestParam int getPage,
            @RequestParam int getNumber,
            @RequestParam String topicId,
            HttpServletRequest request
    ) {
        returnMap.clear();
        paramMap.clear();
        paramMap.put("getPage", getPage);
        paramMap.put("getNumber", getNumber);
        paramMap.put("topicId", topicId);
        paramMap.put("request", request);
        try {
            List<Blog> blogList = topicService.getTopicBlogHot(paramMap);
            returnMap.put("data", blogList);
            returnMap.put("flag", true);
            returnMap.put("msg", "查询成功！");
        } catch (Exception | Error e) {
            returnMap.put("flag", false);
            returnMap.put("msg", "查询失败！");
            logger.error(e);
        }
        return returnMap;
    }
}
